/*
'  DotNetNuke -  http://www.dotnetnuke.com
'  Copyright (c) 2002-2011
'  by DotNetNuke Corporation
' 
'  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
'  documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
'  the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
'  to permit persons to whom the Software is furnished to do so, subject to the following conditions:
' 
'  The above copyright notice and this permission notice shall be included in all copies or substantial portions 
'  of the Software.
' 
'  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
'  TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
'  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
'  CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
'  DEALINGS IN THE SOFTWARE.
*/

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DotNetNuke;
using DotNetNuke.Common;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Modules.Actions;
using DotNetNuke.Security;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Services.Localization;
using DotNetNuke.Modules.Store.Admin;
using DotNetNuke.Modules.Store.Cart;
using DotNetNuke.Modules.Store.Components;
using DotNetNuke.Modules.Store.Providers.Tax;

namespace DotNetNuke.Modules.Store.WebControls
{
    public partial class MiniCart : StoreControlBase
    {
        #region Private Members

        private int itemsCount;

        #endregion

        #region Web Form Designer generated code

        override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
		}

		#endregion

		#region Events

		protected void Page_Load(object sender, System.EventArgs e)
		{
            if (StoreSettings != null)
            {
                try
                {
                    string templatePath = CssTools.GetTemplatePath(this, StoreSettings.PortalTemplates);
                    CssTools.AddCss(this.Page, templatePath, StoreSettings.StyleSheet);

                    // Read module settings and define cart properties
                    DotNetNuke.Modules.Store.Cart.MiniCartSettings cartSettings = new ModuleSettings(ModuleId, TabId).MiniCart;
                    cartControl.ShowThumbnail = cartSettings.ShowThumbnail;
                    cartControl.ThumbnailWidth = cartSettings.ThumbnailWidth;
                    cartControl.GIFBgColor = cartSettings.GIFBgColor;
                    cartControl.EnableImageCaching = cartSettings.EnableImageCaching;
                    cartControl.CacheImageDuration = cartSettings.CacheImageDuration;
                    cartControl.ProductColumn = cartSettings.ProductColumn.ToLower();
                    cartControl.LinkToDetail = cartSettings.LinkToDetail;
                    cartControl.IncludeVAT = cartSettings.IncludeVAT;
                    cartControl.TemplatePath = templatePath;
                    cartControl.ModuleConfiguration = ModuleConfiguration;
                    cartControl.StoreSettings = StoreSettings;
                    cartControl.EditComplete += new EventHandler(cartControl_EditComplete);

                    itemsCount = CurrentCart.GetItems(PortalId, StoreSettings.SecureCookie).Count;
                    if (itemsCount == 0)
                        phlViewCart.Visible = false;
                }
                catch(Exception ex)
                {
                    Exceptions.ProcessModuleLoadException(this, ex);
                }
            }
            else
            {
                if (UserInfo.IsSuperUser)
                {
                    string ErrorSettings = Localization.GetString("ErrorSettings", this.LocalResourceFile);
                    string ErrorSettingsHeading = Localization.GetString("ErrorSettingsHeading", this.LocalResourceFile);
                    DotNetNuke.UI.Skins.Skin.AddModuleMessage(this, ErrorSettingsHeading, ErrorSettings, DotNetNuke.UI.Skins.Controls.ModuleMessage.ModuleMessageType.YellowWarning);
                }
                divControls.Visible = false;
            }
        }

        protected void btnViewCart_Click(object sender, EventArgs e)
		{
            Page.Validate();
            if (Page.IsValid)
                Response.Redirect(Globals.NavigateURL(StoreSettings.ShoppingCartPageID));
		}

        protected void cartControl_EditComplete(object sender, EventArgs e)
        {
            if (cartControl.ItemsCount == 0)
                phlViewCart.Visible = false;
        }

		#endregion
	}
}